import {Injectable}     from '@angular/core';
import {Http, Response, Headers} from '@angular/http';
import {Observable}     from 'rxjs/Observable';

import {Helper}     from './helper';
import {Authenticate}     from './authenticate';
import {Global}     from './global';
import {HttpExt}     from './HttpExt';

import 'rxjs/Rx';
import {AppComponent} from '../app.component'

@Injectable()
export class HTTPService {
    private authenticate: Authenticate;
    constructor(private http: HttpExt) {
        this.authenticate = new Authenticate(http);
    }

    get(url: string, para?: any, data?: (data: any) => void) {
        return this.getServer(url, false, para, data);
    }

    getBlockUI(url: string, para?: any, data?: (data: any) => void) {
        return this.getServer(url, true, para, data);
    }

    private getServer(url: string, blockUI: Boolean, para?: any, data?: (data: any) => void) {
        let body: string
        if(para!=null)
        { 
            body = JSON.stringify(para);
            if (typeof (para) == "string")
                body = para;
        }
        
        let headers = new Headers();

        this.authenticate.setHeaderAuthen(headers);

        if (!Helper.IsNull(body))
            url = url + "?" + body;

        if (blockUI == true) {
            //this._app.showSpin();
        }

        return this.http.get(Global.baseHref + url, { headers: headers })
            .map(d=>d.json())
            .subscribe(
                data,
                error=>{
                    if (blockUI == true) {
                    //this._app.hideSpin();
                    }
                    this.handleError(error)
                },()=>{


                });
    }

    post(url: string, model: any, data?: (data: any) => void) {
        return this.postServer(url, model, false, data);
    }

    postBlockUI(url: string, model: any, data?: (data: any) => void) {
        return this.postServer(url, model, true, data);
    }

    private postServer(url: string, model: any, blockUI: Boolean, data?: (data: any) => void) {
        let body = model;
        
        let headers = new Headers();

        this.authenticate.setHeaderAuthen(headers);

        if (blockUI == true) {
            //this._app.showSpin();
        }

        return this.http.post(Global.baseHref + url, body, { headers: headers })
            .map(d=>d.json())
            .subscribe(
                data,
                error=>{
                    if (blockUI == true) {
                    //this._app.hideSpin();
                    }
                    this.handleError(error)
                },()=>{


                });

    }

    private handleError(error: any) {
        let response = <Response>error;

        if (!Helper.IsNull(response.json)) {
            if (response.status == 401) {
                //this._app.signOut();
            }
            else {
                let js = response.json();
                if (!Helper.IsNull(js.error_description)) {
                    //this._app.showNotification(js.error_description, ENotification.Error);
                    console.error(js.error_description);
                }
            }
        }

        Observable.throw(error);
    }
}